package com.imooc.controller;


import com.imooc.bean.Account;
import com.imooc.bean.AuthResponse;
import com.imooc.bean.AuthResponseCode;
import com.imooc.service.AuthService;
import com.imooc.service.JwtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@Slf4j
public class AuthController implements AuthService {

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @Autowired
    private JwtService jwtService;

    @PostMapping("login")
    public AuthResponse login(
            @RequestParam("username")
            String username,
            @RequestParam("username")
            String pwd
    ){
        Account account = Account.builder().userName(username).build();

        // TODO: 2022/1/22  验证 username+pwd
        String token = jwtService.token(account);
        account.setToken(token);
        account.setRefreshToken(UUID.randomUUID().toString());

        redisTemplate.opsForValue().set(account.getToken(),account);

        return AuthResponse.builder().account(account).code(AuthResponseCode.SUCCESS).build();
    }

    @PostMapping("refresh")
    public AuthResponse refresh(
            @RequestParam("refreshToken")
                    String refreshToken

    ){
        Account account = (Account)redisTemplate.opsForValue().get(refreshToken);
        if (account==null){

            return AuthResponse.builder()
                    .code(AuthResponseCode.USER_NOT_FOUND)
                    .build();
        }
        String token = jwtService.token(account);


        account.setToken(token);
        account.setRefreshToken(UUID.randomUUID().toString());
        redisTemplate.delete(refreshToken);
        redisTemplate.opsForValue().set(account.getToken(),account);

        return AuthResponse.builder().account(account).code(AuthResponseCode.SUCCESS).build();
    }

    private static final String AUTH="Authorization";
    private static final String USERNAME="lhy-user-name";

    @GetMapping("logUser")
    public AuthResponse logUser(@Autowired HttpServletRequest request) {
        String token = request.getHeader(AUTH);
        String username = request.getHeader(USERNAME);
        log.info("当前登录用户-》"+token);
        return this.login( username,"test");
    }





    @GetMapping("verify")
    public AuthResponse verify(
            @RequestParam("token")
                    String token,
            @RequestParam("username")
            String username
    ){

        Boolean verify = jwtService.verify(token, username);
        return AuthResponse.builder().code(
                verify?AuthResponseCode.SUCCESS:AuthResponseCode.USER_NOT_FOUND
        ).build();

    }


}
